package com.goldcontrol.cloudweb.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import com.goldcontrol.cloudweb.mapper.ProjectRepairMapper;
import com.goldcontrol.cloudweb.mapper.ProjectRepairRecordMapper;
import com.goldcontrol.cloudweb.model.ProjectRepair;
import com.goldcontrol.cloudweb.model.ProjectRepairRecord;
import com.goldcontrol.cloudweb.util.FileHandleUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class RepairService {

    private static final String HOST = "https://shunxin.goldcontrol.cn";

    @Autowired
    ProjectRepairMapper projectRepairMapper;

    @Autowired
    ProjectRepairRecordMapper projectRepairRecordMapper;

    public int getRepairId(){
        return projectRepairMapper.getNextId();
    }


    public void addRepair(ProjectRepair repair){
        projectRepairMapper.insert(repair);
    }

    /**
     * 获取用户维修记录
     * @param weixinId
     * @return
     */
    public JSONObject getRepairs(String weixinId,String roleId,String projectId){
        Map<String,Object> param = new HashMap<>();
        if("2".equals(roleId)){
            param.put("weixinId",weixinId);
        }
        param.put("projectId",projectId);
        List<ProjectRepair> repairList = projectRepairMapper.selectByFields(param);
        int unrepaircount = 0;
        if(repairList != null && repairList.size() > 0){
            for(ProjectRepair repair : repairList){
                if(repair.getStatus() == 0){
                    unrepaircount++;
                }
            }
        }
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("repairList",repairList);
        jsonObject.put("unrepaircount",unrepaircount);
        return jsonObject;
    }

    public JSONObject getRepairDetail(int repairId){
        ProjectRepair projectRepair = projectRepairMapper.selectByPrimaryKey(repairId);
        if(projectRepair != null){
            List<Map<String,Object>> fileList = projectRepairMapper.getUploadFiles(repairId);
            projectRepair.setFileList(fileList);
        }
        //获取回复
        List<ProjectRepairRecord> replyList = projectRepairRecordMapper.selectByRepairId(repairId);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("record",projectRepair);
        jsonObject.put("reply",replyList);
        return jsonObject;
    }



    /**
     * 回复功能
     */
    public void addRepairRecord(ProjectRepairRecord repairRecord){
        Map<String,Object> param = new HashMap<>();
        param.put("status",1);
        param.put("repairId",repairRecord.getRepairId());
        projectRepairRecordMapper.updateRepairStatus(param);
        projectRepairRecordMapper.insert(repairRecord);
    }

    /**
     * 获取用户单个维修记录的留言信息
     * @param repairId
     * @return
     */
    public JSONObject getRepairRecord(Integer repairId, Integer userType){
        List<ProjectRepairRecord> recordList = projectRepairRecordMapper.selectByRepairId(repairId);
        JSONObject jsonObject = new JSONObject();
        StringBuffer msg = new StringBuffer();
        for(ProjectRepairRecord repairRecord : recordList){
            if(userType == 0){//普通用户
                if(repairRecord.getUserType() == 0){
                    String itemMsg = "<span style='color:blue;word-wrap: break-word;word-break: break-all;overflow: hidden;'>我:"+repairRecord.getMsg()+"</span><br/>";
                    msg.append(itemMsg);
                }else{
                    String itemMsg = "<span style='color:red;word-wrap: break-word;word-break: break-all;overflow: hidden;'>客服:"+repairRecord.getMsg()+"</span><br/>";
                    msg.append(itemMsg);
                }
            }else{//管理员
                if(repairRecord.getUserType() == 0){
                    String itemMsg = "<span style='color:blue;word-wrap: break-word;word-break: break-all;overflow: hidden;'>客户:"+repairRecord.getMsg()+"</span><br/>";
                    msg.append(itemMsg);
                }else{
                    String itemMsg = "<span style='color:red;word-wrap: break-word;word-break: break-all;overflow: hidden;'>管理员:"+repairRecord.getMsg()+"</span><br/>";
                    msg.append(itemMsg);
                }
            }
        }
        ProjectRepair projectRepair = projectRepairMapper.selectByPrimaryKey(repairId);
        List<Map<String,Object>> fileList = projectRepairMapper.getUploadFiles(repairId);
        jsonObject.put("msg",msg);
        jsonObject.put("phone",projectRepair.getPhone());
        jsonObject.put("username",projectRepair.getUserName());
        jsonObject.put("reason",projectRepair.getReason());
        jsonObject.put("desc",projectRepair.getRepairDesc());
        jsonObject.put("detail","detail");
        jsonObject.put("fileList",fileList);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        jsonObject.put("time",sdf.format(projectRepair.getCreateDttm()));
        return jsonObject;
    }

    public void upoadFile(int repairId, JSONArray files) {
        for (int i = 0; i < files.size(); i++) {
            JSONObject fileObject = files.getJSONObject(i);
            String url = fileObject.getString("url");
            Map<String, Object> param = new HashMap<>();
            param.put("repairId", repairId);
            param.put("url", HOST+url);
            projectRepairMapper.uploadFile(param);
        }
    }

    public String upload(MultipartFile file,String fileName) throws IOException {
        return FileHandleUtil.upload(file.getInputStream(),fileName);
    }

}
